package com.mdlib.helper.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.ljlib.help.R;
import com.mdlib.helper.event.PayModelEvent;
import com.mdlib.helper.ui.UIHelper;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.mdlib.helper.ui.UIHelper.BALANCE;

/**
 * 作者：HQY on 16/11/23 17:09
 * 邮箱：hqy_xz@126.com
 */

public class PayModeDialogFragment extends DialogFragment {


    @Bind(R.id.rb_service_money_balance)
    RadioButton mRbServiceMoneyBalance;
    @Bind(R.id.rl_pay_mode_balance)
    RelativeLayout mRlPayModeBalance;
    @Bind(R.id.rb_service_money_wechat)
    RadioButton mRbServiceMoneyWechat;
    @Bind(R.id.rl_pay_mode_wechat)
    RelativeLayout mRlPayModeWechat;
    @Bind(R.id.rb_service_money_alipay)
    RadioButton mRbServiceMoneyAlipay;
    @Bind(R.id.rl_pay_mode_alipay)
    RelativeLayout mRlPayModeAlipay;
    @Bind(R.id.ll_service_money_back)
    LinearLayout mLlServiceMoneyBack;
    @Bind(R.id.iv_service_money_bg)
    ImageView mIvServiceMoneyBg;
    @Bind(R.id.rl_minnetonka_service_money)
    RelativeLayout mRlMinnetonkaServiceMoney;
    @Bind(R.id.ll_service_money_close)
    LinearLayout mLlServiceMoneyClose;
    private String payType = BALANCE;

    public static PayModeDialogFragment newInstance() {
        return new PayModeDialogFragment();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Dialog_Share);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_pay_mode, null);
        builder.setView(view);
        ButterKnife.bind(this, view);
        return builder.create();
    }

    @Override
    public void onStart() {
        super.onStart();

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick({R.id.rl_pay_mode_balance, R.id.rl_pay_mode_wechat, R.id.rl_pay_mode_alipay, R.id.ll_service_money_back, R.id.ll_service_money_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_pay_mode_balance:
                payType = BALANCE;
                mRbServiceMoneyBalance.setChecked(true);
                mRbServiceMoneyWechat.setChecked(false);
                mRbServiceMoneyAlipay.setChecked(false);
                break;
            case R.id.rl_pay_mode_wechat:
                payType = UIHelper.WECHAT;
                mRbServiceMoneyBalance.setChecked(false);
                mRbServiceMoneyWechat.setChecked(true);
                mRbServiceMoneyAlipay.setChecked(false);
                break;
            case R.id.rl_pay_mode_alipay:
                payType = UIHelper.ALIPAY;
                mRbServiceMoneyBalance.setChecked(false);
                mRbServiceMoneyWechat.setChecked(false);
                mRbServiceMoneyAlipay.setChecked(true);
                break;
            case R.id.ll_service_money_back:
                EventBus.getDefault().post(new PayModelEvent(payType));
                dismiss();
                break;
            case R.id.ll_service_money_close:
                dismiss();
                break;

        }
    }
}
